package leetcode_top;
import org.junit.*;
public class Ex38 {
    class Solution {
        public String countAndSay(int n) {
            if (n == 0) return "";
            String res = "1";
            for (int i = 2; i <= n; i++) {
                res = describe(res.toCharArray());
            }
            return res;
        }   

        public String describe(char[] cs) {
            char last = cs[0];
            int count = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < cs.length; i++) {
                if (cs[i] == last) {
                    count++;
                } else {
                    sb.append(count + "" + last);
                    count = 1;
                    last = cs[i];
                }
            }
            sb.append(count + "" + last);
            return sb.toString();
        }
    }

    @Test
    public void test() {
        Solution s = new Solution();
        System.out.println(s.countAndSay(4));        
    }

    @Test
    public void test2() {
        Solution s = new Solution();
        System.out.println(s.describe("213344".toCharArray()));
    }

    /* 
            输入：n = 4
            输出："1211"
            解释：
            countAndSay(1) = "1"
            countAndSay(2) = 读 "1" = 一 个 1 = "11"
            countAndSay(3) = 读 "11" = 二 个 1 = "21"
            countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
    */
}
